package com.lepu.app.fun.tool.jqtx.remind;

import java.util.Random;

import com.core.lib.utils.main.AppManager;
import com.core.lib.utils.main.LogUtilBase;
import com.core.lib.widget.MyDialog;
import com.eyzhs.app.R;
import com.lepu.app.fun.tool.jqtx.util.ToolJqtxUtil;
import com.lepu.app.main.activity.AppStartActivity;
import com.lepu.app.main.activity.MainTabActivity;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.RemoteViews;

public class RemindReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {

		if (intent == null || TextUtils.isEmpty(intent.getAction())) {
			return;
		}

		LogUtilBase.LogD(null, "收到闹钟广播了    " + intent.getAction());

		if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
			// 设置最近的闹钟
			ToolJqtxUtil.getInstance(context).setJqtxRemind(false,0);
		} else {

			Alarm alarm = (Alarm) intent.getSerializableExtra("alarm");
			if (alarm != null) {

				if(ToolJqtxUtil.getInstance(context).checkRemindIfOpen())
				{
					//通知栏提醒
					Intent openIntent = null;
					if(MainTabActivity.getInstance() == null)
					{
						openIntent = new Intent(context, AppStartActivity.class);
					}else{
						openIntent = new Intent(context, MainTabActivity.class);
					}
					showNotification(context, alarm.note, openIntent);

					//弹出框提醒
					Activity currentActivity = AppManager.getAppManager().currentActivity();
					if(currentActivity != null)
					{
						MyDialog dialog = new MyDialog(currentActivity)
								.setMessage(alarm.note)
								.setPositiveButton(R.string.app_ok, new View.OnClickListener() {

									@Override
									public void onClick(View v) {

									}
								});

						dialog.create(null);
						dialog.showMyDialog();
					}
					//闹钟响过之后
					ToolJqtxUtil.getInstance(context).setJqtxRemind(true,alarm.type);
				}
			}
		}
	}

	@SuppressWarnings("deprecation")
	public void showNotification(Context context, String info, Intent openIntent) {

		NotificationManager notificationManager = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);

		long when = System.currentTimeMillis();

		String title = context.getResources().getString(R.string.app_name);

		Notification notification = new Notification(R.drawable.app_icon,
				title, when);
		notification.defaults = Notification.DEFAULT_SOUND;// 发出默认声音
		notification.flags |= Notification.FLAG_AUTO_CANCEL;// 点击通知后自动清除通知

		RemoteViews rv = new RemoteViews(context.getPackageName(),
				R.layout.app_jqtx_notification);
		rv.setImageViewResource(R.id.ImageView, R.drawable.app_icon);
		rv.setTextViewText(R.id.notification_textview, info);
		PendingIntent contentIntent = PendingIntent.getActivity(context, new Random().nextInt(),
				openIntent, PendingIntent.FLAG_UPDATE_CURRENT);// 当点击消息时就会向系统发送openintent意图

		notification.contentView = rv;
		notification.contentIntent = contentIntent;
		notificationManager.notify(new Random().nextInt(), notification);// 第一个参数为自定义的通知唯一标识
	}
}